package net.maku.needy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.mybatis.service.impl.BaseServiceImpl;
import net.maku.needy.convert.NeedyMemberApplyConvert;
import net.maku.needy.entity.NeedyMemberApplyEntity;
import net.maku.needy.query.NeedyMemberApplyQuery;
import net.maku.needy.vo.NeedyMemberApplyVO;
import net.maku.needy.dao.NeedyMemberApplyDao;
import net.maku.needy.service.NeedyMemberApplyService;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 困难帮扶申请
 *
 * @author 阿沐 babamu@126.com
 * @since 1.0.0 2025-03-20
 */
@Service
@AllArgsConstructor
public class NeedyMemberApplyServiceImpl extends BaseServiceImpl<NeedyMemberApplyDao, NeedyMemberApplyEntity> implements NeedyMemberApplyService {

    private final NeedyMemberApplyDao needyMemberApplyDao;

    @Override
    public PageResult<NeedyMemberApplyVO> page(NeedyMemberApplyQuery query) {
        //IPage<NeedyMemberApplyEntity> page = baseMapper.selectPage(getPage(query), getWrapper(query));
        IPage<NeedyMemberApplyEntity> pageParams = getPage(query);
        QueryWrapper<NeedyMemberApplyVO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(query.getRealName()),"u.real_name",query.getRealName());
        queryWrapper.eq(query.getGender() != null,"u.gender",query.getGender());
        queryWrapper.eq(StringUtils.isNotBlank(query.getOrgName()),"o.name",query.getOrgName());
        queryWrapper.orderByDesc("ma.id");
        IPage<NeedyMemberApplyVO> page = needyMemberApplyDao.selectPageList(pageParams,queryWrapper);
        return new PageResult<>(page.getRecords(),page.getTotal());
    }

    private LambdaQueryWrapper<NeedyMemberApplyEntity> getWrapper(NeedyMemberApplyQuery query){
        LambdaQueryWrapper<NeedyMemberApplyEntity> wrapper = Wrappers.lambdaQuery();
        return wrapper;
    }

    @Override
    public void save(NeedyMemberApplyVO vo) {
        NeedyMemberApplyEntity entity = NeedyMemberApplyConvert.INSTANCE.convert(vo);

        baseMapper.insert(entity);
    }

    @Override
    public void update(NeedyMemberApplyVO vo) {
        NeedyMemberApplyEntity entity = NeedyMemberApplyConvert.INSTANCE.convert(vo);

        updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> idList) {
        removeByIds(idList);
    }

}